<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
namespace core;
defined('BY_JYA') or exit('error');
//系统会员工具函数
class php_ctrl{
	public function edit($params){
		global $_RQ;
		$params['key']=$params['key']?:'id';
		$params['post']=isset($params['post'])?:true;
		$params['message']=$params['message']?:'设置成功！';
		$params['url']=$params['url']?:'list';
		$params['update_message']=$params['update_message']?:$params['message'];
		$params['insert_message']=$params['insert_message']?:$params['message'];
		$params['insert_data']=$params['insert_data']?:array('uniacid'=>$_SESSION['uniacid'],'createtime'=>TIMESTAMP);
		if($params['post'] && $_RQ['postdata']){
			$post=get_postdata();
			$data=array();
			foreach($params['fields'] as $field){
				$data[$field['name']]=is_array($post[$field['name']])?ijson_encode($post[$field['name']]):$post[$field['name']];
				if($field['type']=='time'){
					$data[$field['name']]=strtotime($data[$field['name']]);
				}
			}
			
			$id=intval($post[$params['key']]);
			if($id){
				pdo_update($params['tablename'],$data,array($params['key']=>$id));
				exi($params['update_message'],'',$params['url']);
			}else{
				$data=array_merge($data,$params['insert_data']);
				pdo_insert($params['tablename'],$data);
				exi($params['insert_message'],'',$params['url']);
			}
		}
		$id=intval($_RQ[$params['key']]);
		if($id){
			$item=pdo_get($params['tablename'],array($params['key']=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$item=array();
		}
		$htmls=array(
			'type'=>'edit',
			'desc'=>$params['desc'],
			'edit'=>array(),
		);
		foreach($params['fields'] as $field){
			$htmls['edit'][]=$field;
			$array_types=array('images','pcd','array','array_simple','choose_many','specs','spec_diy','lat_lng','hdps','day','time_range');
			if(in_array($field['type'],$array_types)){
				$item[$field['name']]=$item[$field['name']]?json_decode($item[$field['name']],true):array();
			}
			if($field['type']=='time'){
				$item[$field['name']]=$item[$field['name']]?date('Y-m-d H:i',$item[$field['name']]):'';
			}
		}
		$return['htmls']=$htmls;
		$return['item']=$item;
		if($params['options']){
			$return=array_merge($params['options'],$return);
		}
		exi($return);
	}
	public function del($params){
		global $_RQ;
		$params['key']=$params['key']?:'id';
		$params['message']=$params['message']?:'删除成功！';
		$params['url']=$params['url']?:'list';
		$id=intval($_RQ[$params['key']]);
		if($id){
			pdo_delete($params['tablename'],array($params['key']=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		$del=trim($_RQ['del']);
		if($del){
			$del=str_array($del);
		}
		if($del){
			foreach($del as $id){
				$id=intval($id);
				if($id){
					pdo_delete($params['tablename'],array($params['key']=>$id,'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		exi($params['message'],'',$params['url']);
	}
}